package com.yichen.rent.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import util.IdWorker;

import com.yichen.rent.dao.ProductDao;
import com.yichen.rent.pojo.Product;

/**
 * 服务层
 * 
 * @author Administrator
 *
 */
@Service
public class ProductService {

	@Autowired
	private ProductDao productDao;
	
	@Autowired
	private IdWorker idWorker;

	/**
	 * 查询全部列表
	 * @return
	 */
	public List<Product> findAll() {
		return productDao.findAll();
	}

	
	/**
	 * 条件查询+分页
	 * @param whereMap
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<Product> findSearch(Map whereMap, int page, int size) {
		Specification<Product> specification = createSpecification(whereMap);
		PageRequest pageRequest =  PageRequest.of(page-1, size);
		return productDao.findAll(specification, pageRequest);
	}

	
	/**
	 * 条件查询
	 * @param whereMap
	 * @return
	 */
	public List<Product> findSearch(Map whereMap) {
		Specification<Product> specification = createSpecification(whereMap);
		return productDao.findAll(specification);
	}

	/**
	 * 根据ID查询实体
	 * @param id
	 * @return
	 */
	public Product findById(String id) {
		return productDao.findById(id).get();
	}

	/**
	 * 增加
	 * @param product
	 */
	public void add(Product product) {
		product.setId( idWorker.nextId()+"" );
		productDao.save(product);
	}

	/**
	 * 修改
	 * @param product
	 */
	public void update(Product product) {
		productDao.save(product);
	}

	/**
	 * 删除
	 * @param id
	 */
	public void deleteById(String id) {
		productDao.deleteById(id);
	}

	/**
	 * 动态条件构建
	 * @param searchMap
	 * @return
	 */
	private Specification<Product> createSpecification(Map searchMap) {

		return new Specification<Product>() {

			@Override
			public Predicate toPredicate(Root<Product> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
                // 编号
                if (searchMap.get("id")!=null && !"".equals(searchMap.get("id"))) {
                	predicateList.add(cb.equal(root.get("id").as(String.class), searchMap.get("id")));
                }
                // 房源编号
                if (searchMap.get("house_id")!=null && !"".equals(searchMap.get("house_id"))) {
                	predicateList.add(cb.equal(root.get("house_id").as(String.class), searchMap.get("house_id")));
                }
                // 电视
                if (searchMap.get("television")!=null && !"".equals(searchMap.get("television"))) {
                	predicateList.add(cb.like(root.get("television").as(String.class), "%"+(String)searchMap.get("television")+"%"));
                }
                // 冰箱
                if (searchMap.get("refrigerator")!=null && !"".equals(searchMap.get("refrigerator"))) {
                	predicateList.add(cb.like(root.get("refrigerator").as(String.class), "%"+(String)searchMap.get("refrigerator")+"%"));
                }
                // 洗衣机
                if (searchMap.get("washer")!=null && !"".equals(searchMap.get("washer"))) {
                	predicateList.add(cb.like(root.get("washer").as(String.class), "%"+(String)searchMap.get("washer")+"%"));
                }
                // 空调
                if (searchMap.get("air_conditioner")!=null && !"".equals(searchMap.get("air_conditioner"))) {
                	predicateList.add(cb.like(root.get("air_conditioner").as(String.class), "%"+(String)searchMap.get("air_conditioner")+"%"));
                }
                // 热水器
                if (searchMap.get("calorifier")!=null && !"".equals(searchMap.get("calorifier"))) {
                	predicateList.add(cb.like(root.get("calorifier").as(String.class), "%"+(String)searchMap.get("calorifier")+"%"));
                }
                // 床
                if (searchMap.get("bed")!=null && !"".equals(searchMap.get("bed"))) {
                	predicateList.add(cb.like(root.get("bed").as(String.class), "%"+(String)searchMap.get("bed")+"%"));
                }
                // 暖气
                if (searchMap.get("heating")!=null && !"".equals(searchMap.get("heating"))) {
                	predicateList.add(cb.like(root.get("heating").as(String.class), "%"+(String)searchMap.get("heating")+"%"));
                }
                // 宽带
                if (searchMap.get("broadband")!=null && !"".equals(searchMap.get("broadband"))) {
                	predicateList.add(cb.like(root.get("broadband").as(String.class), "%"+(String)searchMap.get("broadband")+"%"));
                }
                // 衣柜
                if (searchMap.get("closet")!=null && !"".equals(searchMap.get("closet"))) {
                	predicateList.add(cb.like(root.get("closet").as(String.class), "%"+(String)searchMap.get("closet")+"%"));
                }
                // 天然气
                if (searchMap.get("natgas")!=null && !"".equals(searchMap.get("natgas"))) {
                	predicateList.add(cb.like(root.get("natgas").as(String.class), "%"+(String)searchMap.get("natgas")+"%"));
                }
				
				return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));

			}
		};

	}

}
